<?php
declare (strict_types = 1);

namespace app\controller;

use app\middleware\Auth as AuthMiddleware;
use app\model\Cat as CatModel;
use app\model\Info as InfoModel;

use think\Request;

class Info
{
    protected $middleware = [AuthMiddleware::class];
    private $toast = 'public/toast';
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        //
//        return view('index', [
//            'list'  =>   InfoModel::withSearch(['id', 'cat_id', 'title','key', 'des','create_at'], [
//                'id'        =>  request()->param('id'),
//                'cat_id'      =>  request()->param('name'),
//                'title'        =>  request()->param('title'),
//                'key'      =>  request()->param('key'),
//                'des'      =>  request()->param('des'),
//                'create_at'   =>  request()->param('create_at')
//            ])->paginate([
//                'list_rows'     =>  10,
//                'query'         =>  request()->param()
//            ]),
//            'orderTime'         =>  request()->param('create_time') == 'desc' ? 'asc' : 'desc'
//        ]);
        $data=CatModel::with('info')->select();
        return view('index', [
            'list'  => $data->toArray()
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        //

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request,$id)
    {
        //
        // return $data->toArray();
        return $request .$id;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request,$id)
    {
        //
        $data = $request->param();
        if($data['cat_id']){
            return view('edit', [
                'obj'   =>  InfoModel::find($id),
                'cat_id'=> $data['cat_id'],
            ]);
        }else{
            view($this->toast, [
                'infos'     =>  ['未知的错误！'],
                'url_text'  =>  '返回列表页',
                'url_path'  =>  url('/info')
            ]);
        }

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->param();
        if (!$data['logo']){
            $data['logo'] = InfoModel::where('id',$data['id'])->column('logo');
        }
        return InfoModel::update($data) ? view($this->toast, [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/info')
        ]) : '修改失败';
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
